﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class TradeItem : MonoBehaviour {

	//---------------------------------------------------------------------------------------------
	// Properties
	
	public float Price;
	public int itemID;
	
	//---------------------------------------------------------------------------------------------
	// Members 
	
	public PlayerStorage main;
	public SettlementService settleServ;

	public string itemName;
	
	public Text NameText;
	public Text PriceText;
	
	public Text QuantityText;
	public Text QuantityMax;
	
	//---------------------------------------------------------------------------------------------
	// Init
	void Start () 
	{
		settleServ.RetrieveLocalPrices();
		GetPrices();
		CheckAgainstGlobalPrice();
		NameText.text = itemName;
		PriceText.text = "$" + Price.ToString();
		
		
	}
	
	//---------------------------------------------------------------------------------------------
	// Update
	
	//---------------------------------------------------------------------------------------------
	// Core Functions

	void checkItemIDforBuying()
	{
		switch(itemID)
		{
		case 0:
		{
			main.foodQuantity ++;
			break;
		}
		case 1:
		{
			main.waterQuantity ++;
			break;
		}
		case 2:
		{
			main.hoochQuantity ++;
			break;
		}
		case 3:
		{
			main.partsQuantity ++;
			break;
		}
		}
	
	}
	//---------------------------------------------------------------------------------------------
	bool checkItemIDforSelling()
	{
		switch(itemID)
		{
		case 0:
		{
			if (main.foodQuantity >= 1)
			{
				
				main.foodQuantity --;
				return true;
				
			}
			return false;
		}
		case 1:
		{
			if (main.waterQuantity >= 1)
			{
				
				main.waterQuantity --;
				return true;
				
			}
			return false;
		}
		case 2:
		{
			if (main.hoochQuantity >= 1)
			{
				
				main.hoochQuantity --;
				return true;
				
			}
			return false;
		}
		case 3:
		{
			if (main.partsQuantity >= 1)
			{
				
				main.partsQuantity --;
				return true;
				
			}
			return false;
		}
		
		}
		return false;
	}
	//---------------------------------------------------------------------------------------------
	
	public void BuyItem()
	{
		if(main.carry < main.maxcarry)
		{
			if (main.cash >= (0+Price))
			{
				checkItemIDforBuying();
				main.cash += -Price;
				main.carry ++;
			}
			
		}
		
	}
	//---------------------------------------------------------------------------------------------
	public void GetPrices()
	{
		switch(itemID)
		{
			case 0:
			{
				Price = settleServ.localFoodPrice;
				break;
			}
			case 1:
			{
				Price = settleServ.localWaterPrice;
				break;
			}
			case 2:
			{
				Price = settleServ.localHoochPrice;
				break;
			}
			case 3:
			{
				Price = settleServ.localPartsPrice;
				break;
			}
		}
	}
	//---------------------------------------------------------------------------------------------
	public void SellItem()
	{
		if(main.carry > 0)
		{	
			if (checkItemIDforSelling() == true)
			{
				main.cash += Price;
				main.carry --;
			}
		}
		
		
	}
	//---------------------------------------------------------------------------------------------
	void CheckAgainstGlobalPrice()
	{
		
		switch(itemID)
		{
		case 0:
		{
			if (settleServ.globalFoodPrice > Price)
			{
				PriceText.color = Color.green;
			}
			if (settleServ.globalFoodPrice < Price)
			{
				PriceText.color = Color.red;
			}
			if (settleServ.globalFoodPrice == Price)
			{
				PriceText.color = Color.yellow;
			}
			
			break;
		}
		case 1:
		{
			if (settleServ.globalWaterPrice > Price)
			{
				PriceText.color = Color.green;
			}
			if (settleServ.globalWaterPrice < Price)
			{
				PriceText.color = Color.red;
			}
			if (settleServ.globalWaterPrice == Price)
			{
				PriceText.color = Color.yellow;
			}
			break;
		}
		case 2:
		{
			if (settleServ.globalHoochPrice > Price)
			{
				PriceText.color = Color.green;
			}
			if (settleServ.globalHoochPrice < Price)
			{
				PriceText.color = Color.red;
			}
			if (settleServ.globalHoochPrice == Price)
			{
				PriceText.color = Color.yellow;
			}
			break;
		}
		case 3:
		{
			if (settleServ.globalPartsPrice > Price)
			{
				PriceText.color = Color.green;
			}
			if (settleServ.globalPartsPrice < Price)
			{
				PriceText.color = Color.red;
			}
			if (settleServ.globalPartsPrice == Price)
			{
				PriceText.color = Color.yellow;
			}
			break;
		}
		}
	
	}

}
